#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

//点击按钮，进行消息发送。里面的do{…}while用来忽略本窗口，当然自身也可以接受自身的消息。
void MainWindow::onSendMessage()
{
    HWND hwnd = NULL;
    //do
    //{
       LPWSTR path = (LPWSTR)c_strTitle.utf16();  //path = L"SendMessage"
       hwnd = ::FindWindowW(NULL, path);
    //} while (hwnd == (HWND)effectiveWinId()); // 忽略自己

    if (::IsWindow(hwnd))
    {
        QString filename = QStringLiteral("进程通信-Windows消息");
        QByteArray data = filename.toUtf8();

        COPYDATASTRUCT copydata;
        copydata.dwData = CUSTOM_TYPE;  // 用户定义数据
        copydata.lpData = data.data();  //数据大小
        copydata.cbData = data.size();  // 指向数据的指针

        HWND sender = (HWND)effectiveWinId();

        ::SendMessage(hwnd, WM_COPYDATA, reinterpret_cast<WPARAM>(sender), reinterpret_cast<LPARAM>(&copydata));
    }
}

void MainWindow::on_pushButton_clicked()
{
    onSendMessage();
}

MainWindow::~MainWindow()
{
    delete ui;
}

